package br.com.dataphonebr;

import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import br.com.dataphonebr.adapter.AppointmentGroupAdapter;
import br.com.dataphonebr.entity.Grupo;
import br.com.dataphonebr.service.GrupoService;

public class AppointmentGroupListActivity extends GenericActivity<Grupo> implements OnItemClickListener, OnClickListener{

	private GrupoService grupoService;
	private ListView listView;
	
	public AppointmentGroupListActivity() {
		super(Grupo.class);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_appointment_group_list);
		getActionBar().setDisplayHomeAsUpEnabled(true);
		init();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.appointment_group_list, menu);
	    return true;
	} 
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		showProgressBar(AppointmentGroupListActivity.this, null);
		switch (item.getItemId()) {
		    case R.id.addAppointmentGroupActionMenu:
				openView(AppointmentGroupListActivity.this, AppointmentGroupActivity.class, true);
		        return true;
		    case android.R.id.home:
				openView(AppointmentGroupListActivity.this, DashboardActivity.class, true);
		    	return true;
		    default:
		        return super.onOptionsItemSelected(item);
	    }
	}

	
	@Override
	public void init() {
		grupoService = new GrupoService(this);
		setServiceInstance(grupoService);
		super.init();
	}
	
	@Override
	public void prepareComponents() {
		alertDialog = super.createDialog(getString(R.string.optionsLabel), this, getEditRemove(getString(R.string.alert_edit_group), getString(R.string.alert_remove_group)));
		alertYesNo = super.createDialogYesNo(this);
		super.prepareComponents();
	}
	
	public void prepareList(){
		listView = newListView(R.id.groupListView);
		listView.setOnItemClickListener(this);
		adapter = new AppointmentGroupAdapter(grupoService.selectAll());
    	adapter.setDataImage(R.drawable.ic_launcher);
		listView.setAdapter(adapter);
	}

	public void addAppointmentGroupOnClick(View v){
		openView(AppointmentGroupListActivity.this, AppointmentGroupActivity.class, true);
	}

	@Override
	public void onClick(DialogInterface dialog, int item) {
		switch (item) {
			case 0:
				super.loadInstance(AppointmentGroupActivity.class, grupoService.getColumnHack(), getInstance().getCodigo(), true, null);
				break;
			case 1:
				alertYesNo.setMessage(getMensagemExclusao());
				alertYesNo.show();
				break;
			case DialogInterface.BUTTON_POSITIVE:
				delete(getInstance().getCodigo());
				this.prepareList();
				listView.invalidateViews();
				break;
			case DialogInterface.BUTTON_NEGATIVE:
				alertYesNo.dismiss();
				break;
			}		
	}
	
	private String getMensagemExclusao() {
		if (getInstance() != null && getInstance().getNome() != null && !getInstance().getNome().isEmpty())
			return getString(R.string.remove_appointment_group_message, getInstance().getNome());
		return getString(R.string.remove_selected_data);
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		setInstance( (Grupo) listView.getItemAtPosition(position));
		alertDialog.setCanceledOnTouchOutside(true);
		alertDialog.show();
	}

}
